﻿using AutoMapper;
using BaseRepository.Redis;
using Common;
using Common.DataModels;
using Extensions.Attribute.EventAttribute;
using Microsoft.EntityFrameworkCore;
using PLC.Domain.Dto;
using PLC.Domain.Entity;
using PLC.Domain.Enum;
using PLC.Domain.Service.IRepository;

namespace PLC.Domain.Service.Service;

public class DataConfigService
{
    private readonly IDataConfigRepository _dataConfigRepository;
    private readonly IMapper _mapper;
    private readonly IRedisRepository _redisRepository;

    public DataConfigService(IDataConfigRepository dataConfigRepository,IMapper mapper,IRedisRepository redisRepository)
    {
        _dataConfigRepository = dataConfigRepository?? throw new ArgumentNullException(nameof(dataConfigRepository));
        _mapper = mapper?? throw new ArgumentNullException(nameof(mapper));
        _redisRepository = redisRepository?? throw new ArgumentNullException(nameof(redisRepository));
    }

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="pagingModel"></param>
    /// <param name="configType"></param>
    /// <param name="deviceConfigId"></param>
    /// <returns></returns>
    public virtual async Task<Tuple<List<DtoDataConfig>, int>> FindByPageAsync(PagingModel pagingModel, int configType, long deviceConfigId)
    {
        var expression = ExpressionExtensions.True<DataConfig>().And(p => !p.IsDelete);
        // 判读这个枚举中是否 定义了这个值
        if (System.Enum.IsDefined(typeof(EnumConfigType),configType))
        {
            expression = expression.And(p => p.ConfigType == configType);
        }

        if (deviceConfigId!=0)
        {
            expression = expression.And(p => p.DeviceConfigId == deviceConfigId);
        }
        var result =
            await _dataConfigRepository.GetEntitiesByPageAsync(expression, pagingModel.sort, pagingModel.page, pagingModel.rows);
        return new Tuple<List<DtoDataConfig>, int>(_mapper.Map<List<DtoDataConfig>>(result.Item1), result.Item2);
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [Event("DeleteDeviceCache", EnumEventPosition.After)]
    public virtual async Task<DtoDataConfig> UpdateAsync(DtoDataConfig entity)
    {
        var barcodeData = await _dataConfigRepository.GetEntityAsync(p => p.Id == entity.id && !p.IsDelete);
        ArgumentNullException.ThrowIfNull(barcodeData);
        _mapper.Map(entity, barcodeData);
        await _dataConfigRepository.UpdateAsync(barcodeData);
        return entity;
    }
    /// <summary>
    /// 精确查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
   
    public virtual async Task<DtoDataConfig> FindOneAsync(long id)
    {
        var data = await _dataConfigRepository.GetEntityAsync(p => p.Id == id&&!p.IsDelete);
        ArgumentNullException.ThrowIfNull(data);
        return _mapper.Map<DtoDataConfig>(data);
    }
    /// <summary>
    /// 查询所有deviceConfigId包含的dataconfig
    /// </summary>
    /// <param name="deviceConfigIds"></param>
    /// <returns></returns>
    public virtual async Task<List<DataConfig>> FindAll(List<long> deviceConfigIds)
    {
        return await _dataConfigRepository.GetEntitiesAsync(p => !p.IsDelete&&deviceConfigIds.Contains(p.DeviceConfigId)&&p.Status==(int)EnumStatus.启用);
    }
    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [Event("DeleteDeviceCache", EnumEventPosition.After)]
    public virtual async Task<DtoDataConfig> CreateAsync(DtoDataConfig entity)
    {
        var data = _mapper.Map<DataConfig>(entity).Build();
        await _dataConfigRepository.CreateAsync(data);
        return _mapper.Map<DtoDataConfig>(data);
    }
    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [Event("DeleteDeviceCache", EnumEventPosition.After)]
    public virtual async Task DeleteAsync(IEnumerable<long> ids)
    {
        ArgumentNullException.ThrowIfNull(ids);
        await _dataConfigRepository.Qyery.Where(p=>ids.Contains(p.Id)).ExecuteUpdateAsync(p => p.SetProperty(o => o.IsDelete, true));
    }

}